/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.service.bmp.biz.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.xbdun.common.page.Pager;
import com.xbdun.dao.bmp.entity.XbdunRoleDo;
import com.xbdun.dao.bmp.entity.XbdunSysOrgDo;
import com.xbdun.dao.bmp.mapper.RoleMapper;
import com.xbdun.facade.bmp.response.system.RoleResponse;
import com.xbdun.service.bmp.biz.RoleBiz;

import lombok.extern.slf4j.Slf4j;


/**
 * 角色信息
 * @ClassName RoleBizImpl
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
@Service
@Transactional
@Slf4j
public class RoleBizImpl implements RoleBiz {
	@Autowired
	private RoleMapper roleMapper;
	
	/**
	 * 判断角色是否存在,不为0则是存在
	 * @param xbdunRoleDo 新增角色对象
	 * @return int
	 */
	public int existRoleInOrg(Long orgCode, Long xbdunOrgCode, String roleName)throws RuntimeException{
		Assert.isTrue(orgCode>=0,"机构编号不能小于0");
		
		return roleMapper.existRoleInOrg(orgCode,xbdunOrgCode,roleName);
	}
	
	/**
	 * 新增
	 * @param xbdunSysOrgDo 
	 */
	public Long addXbdunRole(XbdunRoleDo xbdunRoleDo) throws RuntimeException{
		Assert.notNull(xbdunRoleDo,"新增xbdunRoleDo对象不能为Null");
		
		return roleMapper.addXbdunRole(xbdunRoleDo);
	}
	
	/**
	 * 更新对象属性
	 * @param xbdunRoleDo	更新角色对象
	 * @param xbdunRoleDo
	 */
	public void updateById(XbdunRoleDo xbdunRoleDo)throws RuntimeException{
		Assert.isTrue(null!=xbdunRoleDo, "更新xbdunRoleDo对象不能为空");
		
		Map<String,Object> param = new HashMap<String,Object>();
		if (!StringUtils.isBlank(xbdunRoleDo.getRoleName())) {
			param.put("roleName", xbdunRoleDo.getRoleName());
		}
		if (!StringUtils.isBlank(xbdunRoleDo.getRoleShort())) {
			param.put("roleShort", xbdunRoleDo.getRoleShort());
		}
		if (!StringUtils.isBlank(xbdunRoleDo.getOptionUser())) {
			param.put("optionUser", xbdunRoleDo.getOptionUser());
		}
		
		Map<String,Object> where = new HashMap<String,Object>();
		where.put("id", xbdunRoleDo.getId());
		
		Timestamp version = roleMapper.getRoleVersion(xbdunRoleDo.getId());
		where.put("version", version);
		
		roleMapper.updateDynXbdunRole(param, where);

		param.clear();
		where.clear();
		param = null;
		where = null;
	}
	
	/**
	 * 根据主键获取对象
	 * @return {@link XbdunSysOrgDo}
	 */
	public XbdunRoleDo getXbdunRoleById(long id)throws RuntimeException{
		Assert.isTrue(id>0, "主键id不能小于零");
		
		return roleMapper.getXbdunRoleById(id);
	}
	/**
	 * 删除数据根据主键
	 * @param idx
	 */
//	public void delXbdunSysOrgById(long id)throws RuntimeException{
//		Assert.isTrue(id>0, "主键id不能小于零");
//		
//		roleMapper.delXbdunSysOrgById(id);
//	}
	public void delXbdunRoleById(long id) throws RuntimeException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 查询分页
	 * @param where			约束条件,键是对象属性其中条件都为and连接
	 * @param orderBy		排序条件
	 * @param pageNum   	当前索引页
	 * @param numPerPage	每页显示条数
	 * @return
	 * @throws RuntimeException
	 */
	public Pager<RoleResponse> queryPager(Map<String,Object> where,String orderBy,int pageIndex,int pageSize) throws RuntimeException {
		//分页对象
		Pager<RoleResponse> resultPager = new Pager<RoleResponse>();
		
		resultPager.setPageSize(pageSize);
		resultPager.setPageIndex(pageIndex);
		resultPager.setSort(orderBy);
		Long resultCnt = roleMapper.queryPagerCount(where);
		if(null!=resultCnt&&resultCnt.longValue()==0){
			
			resultPager.setTotalCount(0);
			return resultPager;
		}
		
		//获取查询结果集List
		List<XbdunRoleDo> list = roleMapper.queryPager(where, orderBy,(resultPager.getPageIndex()-1)*resultPager.getPageSize(), resultPager.getPageSize());
		if(!CollectionUtils.isEmpty(list)){
			resultPager.setData(new ArrayList<RoleResponse>());
			RoleResponse infoRes = null;
			try{
				for(XbdunRoleDo info : list){
					infoRes = new RoleResponse();
					//复制企业列表信息
					BeanUtils.copyProperties(infoRes, info);
					infoRes.setOrgCode(info.getOrgCode());
					resultPager.getData().add(infoRes);
				}
			}catch(Exception e){
				log.error("角色查询组合list异常",e);
			}finally{
				list.clear();
				list = null;
			}
		}

		//设置查询条数
		resultPager.setTotalCount(resultCnt.longValue());
		resultPager.setPager();
		return resultPager;
	}

	public Long getOrgCodeById(long id)  throws RuntimeException {
		Assert.isTrue(id>0,"角色id不能小于0");
		
		return roleMapper.getOrgCodeById(id);	
	}

	public Long deleteRole(Long id, Timestamp version)  throws RuntimeException {
		Assert.isTrue(id>0,"角色id不能小于0");
		
		return roleMapper.deleteRole(id, version);
	}

	public Timestamp getVersionById(Long id) {
		Assert.isTrue(id>0,"角色id不能小于0");
		return roleMapper.getVersionById(id);
	}
}